package org.smart.access.utils;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.smart.access.model.User;
import org.smart.access.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private UserService userService;

    /**
     * 权限注解, 此项目使用住户性质作为权限
     * @param loginId  账号id
     * @param loginType 账号类型
     * @return 住户性质
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .select(User::getResidentNature)
                .eq(User::getUid, loginId)
        );
        return List.of(user.getResidentNature());
    }

    /**
     * 此角色用于区分用户与管理员, 1为住户, 0为管理员
     * @param loginId  账号id
     * @param loginType 账号类型
     * @return 角色
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .select(User::getRole)
                .eq(User::getUid, loginId)
        );
        int role = user.getRole();
        return List.of(String.valueOf(role));
    }
}
